package com.dirtylily.myblog.controller;

import com.dirtylily.myblog.dao.BlogDao;
import com.dirtylily.myblog.pojo.Tag;
import com.dirtylily.myblog.pojo.Type;
import com.dirtylily.myblog.pojo.blog;
import com.dirtylily.myblog.service.BlogService;
import com.dirtylily.myblog.service.TagService;
import com.dirtylily.myblog.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

@Controller
public class indexController {
    @Autowired
    BlogService blogService;
    @Autowired
    BlogDao blogDao;
    @Autowired
    TypeService typeService;
    @Autowired
    TagService tagService;
    @GetMapping("/")
    public String index(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, RedirectAttributes attributes){
        List<Type> topType = typeService.getTopType(6);

        List<Tag> topTag = tagService.getTopTag(6);
        PageHelper.startPage(pageNum,10);
        
        List<blog> at = blogService.getAllBlogByUpdateTime(0);

        PageInfo<blog> pageInfo = new PageInfo<>(at);
        System.out.println("pageInfo:" +pageInfo);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("topTag", topTag);
        model.addAttribute("topType",topType);
        model.addAttribute("recommendedBlog",blogService.getAllRecomendedBlog(8));
        return "index";
    }

    @PostMapping("/search")
    public String search(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,String query){
        List<blog> blogByTitle = blogService.getBlogByTitle(query);

        PageHelper.startPage(pageNum,10);


        PageInfo<blog> pageInfo = new PageInfo<>(blogByTitle);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("query", query);
        return "search";
    }
    @GetMapping("/blog")
    public String blogs(){
        return "blog";
    }

    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model){
        blog blog = blogService.findBlogAndMarkdownToHtml(id);
        model.addAttribute("blog",blog );
        System.out.println(blog);
        return "blog";
    }

    @GetMapping("/footer/newblog")
    public String newblogs(Model model) {
        List<blog> allRecomendedBlog = blogService.getAllRecomendedBlog(3);
        model.addAttribute("newblogs", allRecomendedBlog);
        for (blog blog : allRecomendedBlog) {
            System.out.println(blog);
            System.out.println("===================");
        }
        return "_fragments :: newblogList";
    }

    @GetMapping("/about")
    public String about(){
        return "about";
    }
}
